package com.wwz.leetcode;//[5,0.1%] [20,5.1%],[30,8.4%],[40,15.4%],[100,21.5%],[200,49.5%]

import java.math.BigDecimal;
import java.util.*;
//在一个抽奖活动中，用户抽奖必中，一共有6种奖品，每种奖品得数量和中奖概率分别为
//[5,0.1%1，[20,5.1%].[30,8.4%]，[40, 15.4%]， [100,21.5%]，[200,49.5%]
//如果某一次抽奖抽中一类库存耗光的奖品后，则需要去除当前奖品后重新用剩余奖品得权重再算奖。
//请写一段代码来模拟这个抽奖，要求实际中奖概率可以基本符合设置要求
public class LuckDraw {

    public static void main(String[] args) {
        // 概率数组
        List<Integer> probabilityList = new ArrayList<>();
        // 新的概率
        List<Integer> newProbabilityList = new ArrayList<>();
        // 奖品数Map
        Map<Integer, Integer> rewardCountMap = new HashMap<>();
        // 初始化概率
        Initprobability(probabilityList, rewardCountMap);
        boolean flag = true;
        Random random = new Random();

        // 循环
        while (true) {
            Integer reward = 0;
            if (flag) {
                int index = random.nextInt(probabilityList.size());
                reward = probabilityList.get(index);
            } else {
                int index = random.nextInt(newProbabilityList.size());
                reward = newProbabilityList.get(index);
            }

            if (0 != rewardCountMap.get(reward)) {
                rewardCountMap.put(reward, rewardCountMap.get(reward) - 1);
            } else {
                // 把奖品弄出去
                rewardCountMap.remove(reward);
                newProbabilityList.clear();
                // 重新弄概率  遍历map 根据count 重新计算概率
                flag = false;
                for (Map.Entry<Integer, Integer> entry : rewardCountMap.entrySet()) {
                    if (0 != entry.getValue()) {
                        for (int i = 0; i < entry.getValue(); i++) {
                            newProbabilityList.add(entry.getKey());
                        }
                    }
                }
                int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;
                for (Integer i : newProbabilityList) {
                    if (i.equals(1)) {
                        one++;
                    }
                    if (i.equals(2)) {
                        two++;
                    }
                    if (i.equals(3)) {
                        three++;
                    }
                    if (i.equals(4)) {
                        four++;
                    }
                    if (i.equals(5)) {
                        five++;
                    }
                    if (i.equals(6)) {
                        six++;
                    }
                }
                if(one == 0 && two == 0 && three == 0 && four == 0 && five == 0 && six == 0){
                    break;
                }

                System.out.println("一等奖概率是" + BigDecimal.valueOf(one).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("二等奖概率是" + BigDecimal.valueOf(two).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("三等奖概率是" + BigDecimal.valueOf(three).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("四等奖概率是" + BigDecimal.valueOf(four).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("五等奖概率是" + BigDecimal.valueOf(five).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("六等奖概率是" + BigDecimal.valueOf(six).divide(BigDecimal.valueOf(newProbabilityList.size()),2,BigDecimal.ROUND_HALF_UP));
                System.out.println("========================");
            }

        }
    }

    private static void Initprobability(List<Integer> probabilityList, Map<Integer, Integer> rewardCountMap) {

        rewardCountMap.put(1, 5);
        rewardCountMap.put(2, 20);
        rewardCountMap.put(3, 30);
        rewardCountMap.put(4, 40);
        rewardCountMap.put(5, 100);
        rewardCountMap.put(6, 200);

        for (int i = 0; i < 1; i++) {
            probabilityList.add(1);
        }
        for (int i = 0; i < 51; i++) {
            probabilityList.add(2);
        }
        for (int i = 0; i < 84; i++) {
            probabilityList.add(3);
        }
        for (int i = 0; i < 154; i++) {
            probabilityList.add(4);
        }
        for (int i = 0; i < 215; i++) {
            probabilityList.add(5);
        }
        for (int i = 0; i < 495; i++) {
            probabilityList.add(6);
        }
    }
}
